package com.nl.controller.common.annotation;

import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.global.GlobalEnum;
import com.nl.controller.common.mapper.CommonMapper;
import com.nl.controller.common.model.OperatorLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@Slf4j
public class SystemLogAspect {

    @Autowired
    private CommonMapper commonMapper;

    @Pointcut("@annotation(com.nl.controller.common.annotation.SystemServiceLog)")
    public void serviceAspect() {
    }

    @Around("serviceAspect()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Exception {

        log.debug("**********环绕通知开始**********");

        Object object;
        long methodStart = System.currentTimeMillis();
        try {
            object = joinPoint.proceed();
        } catch (Throwable e) {
            log.error("环绕通知：方法调用出现异常***");
            e.printStackTrace();
            System.out.println(e.getMessage());
            throw new SmsException(GlobalEnum.UNKOWN_ERROR);
        }
        long methodEnd = System.currentTimeMillis();
        log.info("方法执行耗时:{}", (methodEnd - methodStart));

        this.getInit(joinPoint, methodEnd - methodStart);

        return object;
    }

    private void getInit(ProceedingJoinPoint joinPoint, long time) {

        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String address = request.getRemoteAddr();

        String methodPath = joinPoint.getSignature().getDeclaringTypeName() +
                "." + joinPoint.getSignature().getName();

        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
        boolean value = ms.getMethod().getAnnotation(SystemServiceLog.class).value();
        String description = ms.getMethod().getAnnotation(SystemServiceLog.class).description();

        if (value) {
            OperatorLog operatorLog = new OperatorLog();
            operatorLog.setOperatorId("1000");
            operatorLog.setOperatorName("test");
            operatorLog.setMethodPath(methodPath);
            operatorLog.setAddress(address);
            operatorLog.setRemark(description);
            operatorLog.setConsume(time + "");
            log.debug("操作日志参数:{}",operatorLog.toString());
            try {
                commonMapper.createOperatorLog(operatorLog);
            } catch (Exception e){
                e.printStackTrace();
                throw new SmsException();
            }
        }
    }
}
